package com.lyr.query.dynamic.core.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lyr.query.dynamic.core.domain.QueryCondition;
import com.lyr.query.dynamic.core.domain.QueryConfig;
import com.lyr.query.dynamic.core.domain.req.QueryReq;
import com.lyr.query.dynamic.core.service.ConfigService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态查询控制器
 *
 * @author zk
 * @date 2025/2/7
 */
@RestController
@RequestMapping("/dynamic/query")
public class QueryController {

    @Resource
    private ConfigService configService;

    /**
     * 获取查询条件
     *
     * @param req 请求
     * @return {@link ResponseEntity}<{@link List}<{@link QueryCondition}>>
     */
    @GetMapping("/getConditions")
    public ResponseEntity<List<QueryCondition>> getConditions(QueryReq req) {
        QueryConfig config = configService.lambdaQuery().eq(Objects.nonNull(req.getPath()),QueryConfig::getPath, req.getPath())
                            .eq(Objects.nonNull(req.getUserId()),QueryConfig::getUserId, req.getUserId())
                            .eq(Objects.nonNull(req.getTenantId()),QueryConfig::getTenantId, req.getTenantId())
                            .one();
        if (Objects.isNull(config)){
            return ResponseEntity.ok(Collections.emptyList());
        }
        List<QueryCondition> conditionList = new ArrayList<>();
        for (Object obj : config.getConditionsJson()) {
            if (obj instanceof Map) {
                // 将 LinkedHashMap 转换为 QueryCondition 对象
                @SuppressWarnings("unchecked")
                Map<String, Object> map = (Map<String, Object>) obj;
                QueryCondition queryCondition = new QueryCondition();
                // 这里假设 "field" 是 JSON 中的键名，请根据实际情况调整
                if(map.containsKey("field")) {
                    queryCondition.setField((String) map.get("field"));
                }
                if(map.containsKey("operator")) {
                    queryCondition.setOperator((String) map.get("operator"));
                }
                if(map.containsKey("value")) {
                    queryCondition.setValue(map.get("value"));
                }
                if(map.containsKey("valueType")) {
                    queryCondition.setValueType((String) map.get("valueType"));
                }
                if(map.containsKey("tableAlias")) {
                    queryCondition.setTableAlias((String) map.get("tableAlias"));
                }
                //把下划线转驼峰

                queryCondition.setField(StringUtils.underlineToCamel(queryCondition.getField()));
                conditionList.add(queryCondition);
            } else if (obj instanceof QueryCondition) {
                // 如果已经是 QueryCondition 类型，则直接添加
                conditionList.add((QueryCondition) obj);
            }
        }
        return ResponseEntity.ok(conditionList);
    }
}
